<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Billboard_model extends Model 
{
    var $table  = FALSE;
    var $tables = FALSE;
    var $fields = FALSE;

	private function setVars(){
        $this->tables = array(
                                'billboard_vehicle',
                            );
	}
	
	public function __construct()
	{     
		parent::Model();
	}

    public function setTable($table)
    {
        $this->table = $table;
    }

    public function getFields()
    {
        $f = $this->db->list_fields($this->table);
        foreach ($f as $field) $this->fields[$field] = '';
        return $this->fields;
    }
	
    public function getVehicles( $org )
    {
        $this->db->where('org_id', $org);
        $query = $this->db->get('billboard_vehicle');
        
        if ( $query->num_rows() > 0 ){
            return $query->result();
        }else{
            return FALSE;
        }
    }

    public function getVehicle( $id )
    {
        $this->db->select('Name, Showroom, image');
        $this->db->where('id', $id);
        $query = $this->db->get('billboard_vehicle');
        
        if ( $query->num_rows() > 0 ) {
           $row = $query->row();
           $row->thumb = $row->image;
           $row->image = str_replace('_thumb','', $row->image);
           return $row;
        }else{
            return FALSE;
        }
    }


    public function save( $postarray, $by = 'id' )
    {
        if(isset($postarray['thumb'])){
            $postarray['image'] = $postarray['thumb'];
            unset($postarray['thumb']);
        }
        $this->getFields();
        $i = 0;
        foreach($this->fields as $k => $v){
            if(isset($postarray[$k])){
                $this->db->set($k, $postarray[$k]);
                $i++;
            }
        }
        if($i > 0){
            if( ! isset($postarray['id']) && $by == 'id' ) {
                $this->db->insert($this->table); 
                return $this->db->insert_id();
            }else{
                if ( isset( $postarray['id'] ) ) {
                    $this->fun->printa($postarray);
                    $w = $this->db->where('id', $postarray['id']);
                    //$p = (object) $postarray;
                    //$sql = "UPDATE `billboard_vehicle` SET Showroom = '$p->Showroom' WHERE id = '$p->id'";
                    //$this->db->query($sql);
                    //$this->fun->printa($w);
                    //return true;
                    //return $this->db->update($this->table);
                }elseif ( $by != 'id' && $this->keycheck( $by, $postarray ) ) {
                    $this->msg->setMsg('ok');
                    $this->db->where($by, $postarray[$by]);
                    $this->fun->printa($postarray);
                    //return $this->db->update($this->table);
                }
            }
        }
        return FALSE;
    }
    
    public function keycheck($string, $postarray)
    {
        foreach($postarray as $k => $v){
            if($string == $k){
                return TRUE;
            }
        }
        return FALSE;
    }
    
    public function delete($key, $by = 'id' )
    {
        $this->db->where($by, $key);
        $this->db->delete($this->table); 
    }
}
?>
